﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;

namespace THO7AlgoritmTimerApplication
{
    class VisionAlgorithm_Group_1 : VisionAlgorithm
    {

        public VisionAlgorithm_Group_1(String name) : base(name) { }

        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Image srcImg = new Image(sourceImage);
            Image destImg = new Image(sourceImage.Height, sourceImage.Width);

            srcImg.Lock();
            destImg.Lock();

            int medianSize = 3;
            int medianSizeHalf = (medianSize / 2) + 1;
            bool filterYEnable = false;

            for (int y = 0; y < sourceImage.Height; y++)
            {
                if ((y > medianSizeHalf) && (y < sourceImage.Height - medianSizeHalf - 1))
                {
                    filterYEnable = true;
                }
                else
                {
                    filterYEnable = false;
                }
                for (int x = 0; x < sourceImage.Width; x++)
                {
                    if ((x > medianSizeHalf) && (x < sourceImage.Width - medianSizeHalf - 1))
                    {
                        if (filterYEnable)
                        {
                            //PixelColor color = srcImg.GetPixel(x, y);
                            //destImg.SetPixel(x, y, color);
                            byte[] filterPixels = new byte[medianSize * medianSize];
                            for (int filterY = 0; filterY < medianSize; filterY++)
                            {
                                int curY = y - medianSizeHalf;
                                for (int filterX = 0; filterX < medianSize; filterX++)
                                {
                                    int curX = x - medianSizeHalf;
                                    PixelColor color = srcImg.GetPixel(curX + filterX, curY + filterY);
                                    int red = srcImg.GetPixelR(curX + filterX, curY + filterY);
                                    filterPixels[(filterY * medianSize) + filterX] = color.R;
                                }
                            }
                            QuickSort<byte> sort = new QuickSort<byte>(filterPixels);
                            sort.Sort();
                            PixelColor color1 = new PixelColor(0, 0, 0);
                            color1.R = sort.Output[(medianSize * medianSize) / 2 + 1];
                            destImg.SetPixel(x, y, color1);
                        }
                        else
                        {
                            PixelColor color = new PixelColor(0, 255, 0);
                            destImg.SetPixel(x, y, color);
                        }
                    }
                    else
                    {
                        PixelColor color = new PixelColor(0, 255, 0);
                        destImg.SetPixel(x, y, color);
                    }
                }
            }

            srcImg.Unlock();
            return destImg.Unlock();
        } 
    }
}
